package com.kingdee.pda.util;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.GlideModule;
import com.kingdee.pda.plugins.crash.CacheDirectoryManager;
import com.kingdee.pda.plugins.glide.OkHttpUrlLoader;
import com.kingdee.pda.plugins.glide.SSLSocketClient;

import java.io.File;
import java.io.InputStream;

import okhttp3.OkHttpClient;

/**
 * Created by zhanlg
 * DATE 2018/3/27
 * TIME 13:52
 */

public class MyGlideModule implements GlideModule {


    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        final int diskCacheSizeBytes = 1024 * 1024 * 100; // 100MB
        //        设置内存缓存大小
        builder.setMemoryCache(new LruResourceCache(diskCacheSizeBytes));
        builder.setDiskCache(new DiskCache.Factory(){
            @Override
            public DiskCache build() {
                File file = new File(CacheDirectoryManager.getInstance().getExternCacheImagePath());
                file.mkdirs();
                return DiskLruCacheWrapper.get(file, diskCacheSizeBytes);
            }
        });
        builder.setDecodeFormat(DecodeFormat.PREFER_RGB_565);

    }

    @Override
    public void registerComponents(Context context, Glide glide) {
        OkHttpClient build = new OkHttpClient().newBuilder()
                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                .build();
        glide.register(GlideUrl.class, InputStream.class,new OkHttpUrlLoader.Factory(build));
    }
}
